package com.fivestar.finance.app.ui.main;

import android.app.Fragment;
import android.content.Context;
import android.view.MenuItem;

import com.fivestar.finance.app.R;
import com.fivestar.finance.app.core.AppManager;
import com.fivestar.finance.app.core.mvp.BasePresenter;
import com.fivestar.finance.app.injector.scope.ActivityScope;
import com.fivestar.finance.app.ui.box.HomeBoxFragment;
import com.fivestar.finance.app.ui.iou.IouFragment;
import com.fivestar.finance.app.ui.message.MessageFragment;
import com.fivestar.finance.library.utils.ToastUtils;

import javax.inject.Inject;

/**
 * 简要描述
 *  
 *  @author Derek.Kidd
 * @version V1.0.0
 * @date 2016/8/23 10:41
 */
@ActivityScope
public class MainPresenter extends BasePresenter<IMainView> {

    private long mExitTime = 0;

    @Inject
    public MainPresenter() {

    }

    public void onNavigationClick(MenuItem item) {

        Fragment mFragment = null;
        switch (item.getItemId()) {
            case R.id.nav_box:
                mFragment =new HomeBoxFragment();
                break;
            case R.id.nav_message:
                mFragment =new MessageFragment();
                break;
            case R.id.nav_iou:
                mFragment =new IouFragment();
                break;
            case R.id.nav_cart:
                //TODO
                return;
            case R.id.nav_setting:
                getMvpView().showSettingUi();
                break;
            case R.id.nav_feedback:
                getMvpView().showFeedBackUi();
                break;
            case R.id.nav_about:
                getMvpView().showAboutUi();
                break;
        }

        if (mFragment != null) {
            item.setChecked(true);
            getMvpView().setTitle(item.getTitle());
            getMvpView().showFragment(mFragment);
        }
        getMvpView().closeDrawers();
    }


    public void exist() {
        if (isCanExit()) {
            AppManager.getAppManager().AppExit((Context) getMvpView());
        }
    }

    private boolean isCanExit() {
        if (System.currentTimeMillis() - mExitTime > 2000) {
            ToastUtils.show((Context) getMvpView(), "再按一次退出程序", ToastUtils.LENGTH_LONG);

            mExitTime = System.currentTimeMillis();
            return false;
        }
        return true;
    }

}
